/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package carstore.orderserver.controllers;
import carstore.orderserver.models.OrderInfo;
import carstore.orderserver.models.OrderInfoExt;
import carstore.orderserver.services.OrderRepository;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Calendar;

/**
 *
 * @author wuhao
 */
@RestController
public class OrderController {
    @Autowired
    private OrderRepository OrderRepositoryImpl;

    @RequestMapping("/ordercount")
    public long getordercount()
    {
        return OrderRepositoryImpl.count();
    }
    @RequestMapping("/order")
    public List<OrderInfo> getorderByID(BigInteger orderId)
    {
        return OrderRepositoryImpl.findByorderId(orderId);
    }

    @RequestMapping("/allorders")
    public List<OrderInfo> getall()
    {
        return (List<OrderInfo>) OrderRepositoryImpl.findAll();
    }

    @RequestMapping("/ordersbyuser")
    public List<OrderInfoExt> getOrderInfobyUserId(Integer UserId)
    {
        //System.out.println("get orders by user");
        //List<OrderInfo> OrderList=(List<OrderInfo>) OrderRepositoryImpl.findByuserId(UserId);
        List<Object[]> OrderDetails=OrderRepositoryImpl.findOrdersByUserId(UserId);
        List<OrderInfoExt> OrderList= new ArrayList<OrderInfoExt>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(int i=0;i<OrderDetails.size();i++)
        {
            Object[] obj=OrderDetails.get(i);
            OrderInfoExt orderinfo=new OrderInfoExt();
            orderinfo.setOrderId(new BigInteger(obj[0].toString().getBytes()));
            orderinfo.setStrOrderId(obj[0].toString());
            Date date=new Date();
            try {
                date=formatter.parse(obj[1].toString());
            } catch (ParseException e) {
            }
            orderinfo.setOrderTime(date);
            orderinfo.setStatus(obj[2].toString());
            orderinfo.setStoreId(Integer.parseInt(obj[3].toString()));
            orderinfo.setStoreName(obj[4].toString());
            orderinfo.setCarID(Integer.parseInt(obj[5].toString()));
            orderinfo.setFactoryId(obj[6].toString());
            orderinfo.setCarName(obj[7].toString());
            orderinfo.setUserName(obj[8].toString());
            orderinfo.setUserId(Integer.parseInt(obj[9].toString()));
            OrderList.add(orderinfo);
            
            //System.out.println(orderinfo);
        }
        //System.out.println("get orders by user-----end");
        return OrderList;
    }

    @RequestMapping("/neworder")
    @ResponseBody
    public void setNewOrder(BigInteger orderId, String orderTime, int storeId, int carID, int userId, String orderStatus){
        //public void setNewOrder(BigInteger orderId, Date orderTime, int storeId, int carID, int userId, String orderStatus){
        OrderInfo orderinfo=new OrderInfo();
        orderinfo.setOrderId(orderId);
        orderinfo.setCarID(carID);
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        try {
            date=formatter.parse(orderTime);
        } catch (ParseException e) {
            System.out.println("新建订单错误：订单日期不正确");
        }
        orderinfo.setOrderTime(date);
        orderinfo.setStatus(orderStatus);
        orderinfo.setStoreId(storeId);
        orderinfo.setUserId(userId);

        OrderRepositoryImpl.save(orderinfo);
        }

    @RequestMapping("/paybyorderid")
    public void setPayOrderStatus(BigInteger orderId)
    {
        List<OrderInfo> OrderInfoList=OrderRepositoryImpl.findByorderId(orderId);
        if(OrderInfoList.size()!=0)
        {
            OrderInfo orderinfo=OrderInfoList.get(0);
            orderinfo.setStatus("已支付");
            OrderRepositoryImpl.save(orderinfo);
        }
    }

    }
